package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.data.Page;
import com.dongdong.data.Result;
import com.dongdong.em.ResultEmnu;
import com.dongdong.pojo.TbSpecification;
import com.dongdong.pojo.TbSpecificationOption;
import com.dongdong.service.SpecificationService;
import com.dongdong.service.TbSpecificationOptionService;
import com.dongdong.vo.SpecVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("specificationController")
public class SpecificationController {
    @Reference
    private SpecificationService specificationService;
    @Reference
    private TbSpecificationOptionService tbSpecificationOptionService;

    @RequestMapping("toSpecification")
    public String toSpecification(){
        return "admin/specification";
    }
    @RequestMapping("specificationList")
    @ResponseBody
    public Result specificationList(String specName,@RequestParam(defaultValue = "1")Integer pageNum,
                                    @RequestParam(defaultValue = "5")Integer pageSize) {
        Page<TbSpecification> list = specificationService.selectInfo(pageNum, pageSize, specName);
        try {
            return Result.respone(ResultEmnu.SUCCESS).setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.respone(ResultEmnu.ERROR).setData(list);
        }
    }

    @RequestMapping("deleteByIds/{ids}")
    @ResponseBody
    public Result deleteAll(@PathVariable("ids")Long[] ids){
        int i = specificationService.deletebyIds(ids);
        if (i>0){
            return Result.respone(ResultEmnu.SUCCESS);
        }else {
            return Result.respone((ResultEmnu.SUCCESS));
        }
    }
    @RequestMapping("toUpdate/{id}")
    @ResponseBody
    public Result toUpdate(@PathVariable("id") Long id){
        SpecVO specVO = null;
        try {
            specVO = specificationService.selectById(id);
            return Result.respone(ResultEmnu.SUCCESS).setData(specVO);
        }catch (Exception e){
            e.printStackTrace();
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("increasInfo")
    @ResponseBody
    public Result increasInfo(String options,TbSpecification tbSpecification) {
        //将数组转为集合
        List<TbSpecificationOption> specOptionList = JSONObject.parseArray(options, TbSpecificationOption.class);
        int i = specificationService.increaseInfo(tbSpecification, specOptionList);
        if (i > 0) {
                return Result.respone(ResultEmnu.SUCCESS);
            } else  {
                return Result.respone(ResultEmnu.ERROR);
            }
    }
    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbSpecification tbSpecification,String optionsListJson){
        //将数组改集合
        List<TbSpecificationOption> optionList = JSONObject.parseArray(optionsListJson,TbSpecificationOption.class);
        int i =  specificationService.updateSpecifiction(tbSpecification,optionList);
        if(i > 0){
            return Result.respone(ResultEmnu.SUCCESS);
        }
        return Result.respone(ResultEmnu.ERROR);

    }
}





